package com.surprise.base.util;

import java.util.Collection;
import java.util.Map;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.surprise.base.bo.Triple;

/**
 * FlatUtils.java
 * @author nick
 * 2015年3月3日
 */
public class FlatUtils {
    public static <F, S, T> Map<F, Multimap<S, T>> tri2tup(Collection<Triple<F, S, T>> triples) {
        Map<F, Multimap<S, T>> ret = Maps.newHashMap();
        for(Triple<F, S, T> triple : triples) {
            Multimap<S, T> multimap = null;
            if(ret.containsKey(triple.getF())) {
                multimap = ret.get(triple.getF());
            } else {
                multimap = ArrayListMultimap.create();
                ret.put(triple.getF(), multimap);
            }
            multimap.put(triple.getS(), triple.getT());
        }
        return ret;
    }
    
    public static void main(String[] args) {
        Collection<Triple<Long, Long, Long>> triples = Lists.newArrayList();
        for(long f = 0 ; f < 10 ; f++) {
            for(long s = 0; s<5 ; s++) {
                for(long t = 0; t<2; t++) {
                    triples.add(new Triple<Long, Long, Long>(f, s, t));
                }
            }
        }
        Map<Long, Multimap<Long, Long>> ret = tri2tup(triples);
        System.out.println(ret);
    }
}
